package com.voute.Utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.voute.App.App;


/**
 * Preference操作工具类
 * Created by liukanshan on 2017/1/1.
 */

public class PreferenceUtils {

    public static String GetUserName() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        return preferences.getString("user_name_key", "");
    }


    public static String GetUserPassword() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        return preferences.getString("password_key", "");
    }

    public static String GetUserToken() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        return preferences.getString("token_key", "");
    }

    public static void SaveUserName(String user_name) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("user_name_key", user_name);
        editor.commit();
    }

    public static void SaveUserPassword(String password) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("password_key", password);
        editor.commit();
    }

    public static void SaveUserToken(String token) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("token_key", token);
        editor.commit();
    }

    public static void SaveDefaultUserFirstUse(boolean flag) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("default_user_first_use", flag);
        editor.commit();
    }

    public static boolean GetDefaultUserFirst(){
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        boolean first_use = preferences.getBoolean("default_user_first_use", false);
        return first_use;
    }



    public static void SaveUserIdHash(String user_hash) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("user_id_hash_key", user_hash);
        editor.commit();
    }

    public static String GetUserIdHash(){
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String user_hash = preferences.getString("user_id_hash_key", "");
        return user_hash;
    }

    public static void ClearUserInfo() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove("user_name_key");
        editor.remove("password_key");
        editor.remove("token_key");
        editor.remove("user_id_hash_key");
        editor.commit();
    }

    /**
     * 保存经纬度
     *
     * @param latitude
     * @param longtitude
     */
    public static void SaveLocation(String latitude, String longtitude) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("latitude", latitude);
        editor.putString("longtitude", longtitude);
        editor.commit();
    }

    public static String GetLatitude() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String latitude = preferences.getString("latitude", "");
        if (TextUtils.isEmpty(latitude)) {
            return 31.3403030000 + "";
        }
        return latitude;
    }

    public static String GetLongtitude() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String longtitude = preferences.getString("longtitude", "");
        if (TextUtils.isEmpty(longtitude)) {
            return 121.3764190000 + "";
        }
        return longtitude;
    }

    public static void SaveRSAKey(String key) {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("rsa_key", key);
        editor.commit();
    }

    public static String GetRSAKey() {
        SharedPreferences preferences = App.GetInstance().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        return preferences.getString("rsa_key", "");
    }
}
